package cn.wapache.i18n;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

public class DefaultI18nService implements I18nService {

	private I18nEntry EMPTY = new DefaultI18nEntry();
	
	private Map<String,I18nEntry> entryMap = new HashMap<>();
	
	private String name;
	private String description;
	
	@Override
	public I18nEntry get(String key) {
		return entryMap.get(key);
	}

	@Override
	public Optional<I18nEntry> getOptional(String key) {
		return Optional.ofNullable(entryMap.get(key));
	}

	@Override
	public I18nEntry getOrDefault(String key, I18nEntry def) {
		return entryMap.getOrDefault(key, def);
	}

	@Override
	public String getDescription(String key, Object... values) {
		return entryMap.getOrDefault(key, EMPTY).getDescription(values);
	}

	public DefaultI18nService put(I18nEntry entry){
		entryMap.put(entry.getKey(), entry);
		return this;
	}
	
	public DefaultI18nService putAll(I18nEntry... entries){
		
		for (I18nEntry entry : entries) {
			entryMap.put(entry.getKey(), entry);
		}
		
		return this;
	}

	public DefaultI18nService putAll(Collection<I18nEntry> entries){
		
		for (I18nEntry entry : entries) {
			entryMap.put(entry.getKey(), entry);
		}
		
		return this;
	}

	public String getName() {
		return name;
	}

	public DefaultI18nService setName(String name) {
		this.name = name;
		return this;
	}

	public String getDescription() {
		return description;
	}

	public DefaultI18nService setDescription(String description) {
		this.description = description;
		return this;
	}
	
}
